<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="园区运营公司" prop="gardenName">
        <el-input
          v-model="queryParams.gardenName"
          placeholder="请输入园区运营公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一信用编码" prop="gardenCode">
        <el-input
          v-model="queryParams.gardenCode"
          placeholder="请输入统一信用编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="gardenPerson">
        <el-input
          v-model="queryParams.gardenPerson"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属街道" prop="parentName">
        <el-input
          v-model="queryParams.parentName"
          placeholder="请输入所属街道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属园区" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入所属园区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:aqjcGardenInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:aqjcGardenInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:aqjcGardenInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:aqjcGardenInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aqjcGardenInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="所属街道" align="center" prop="parentName" width="100" show-overflow-tooltip/>
      <el-table-column label="所属园区" align="center" prop="deptName" />
      <el-table-column label="园区图片" align="center" prop="parkImage" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.parkImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="园区运营公司" align="center" prop="gardenName" show-overflow-tooltip/>
      <el-table-column label="统一信用编码" align="center" prop="gardenCode" width="100" show-overflow-tooltip/>
<!--      <el-table-column label="人数" align="center" prop="gardenNum" />-->
      <el-table-column label="地址" align="center" prop="gardenAddress" show-overflow-tooltip/>
      <el-table-column label="负责人" align="center" prop="gardenPerson" />
      <el-table-column label="联系方式" align="center" prop="gardenPhone" width="110"/>
      <el-table-column label="安全人员" align="center" prop="safePerson" />
      <el-table-column label="联系方式" align="center" prop="safePhone" width="110"/>
      <!-- <el-table-column label="经度" align="center" prop="lon" />
      <el-table-column label="纬度" align="center" prop="lat" />
      <el-table-column label="创建" align="center" prop="createId" />
      <el-table-column label="更新" align="center" prop="updateId" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:aqjcGardenInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:aqjcGardenInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改园区基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="园区图片" prop="parkImage">
          <image-upload v-model="form.parkImage"/>
        </el-form-item>
        <el-form-item label="归属园区" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择" />
        </el-form-item>
<!--        <el-form-item label="所属园区" prop="deptId">-->
          <!-- <el-cascader :placeholder="form.deptName" :props="props" @change="changeXqid"></el-cascader> -->
<!--          <el-cascader-->
<!--            v-model="cascaderValue"-->
<!--            :placeholder="form.deptName"-->
<!--            :props="props"-->
<!--            @change="changeXqid"-->
<!--          ></el-cascader>-->
<!--        </el-form-item>-->
        <el-form-item label="园区运营公司" prop="gardenName">
          <el-input v-model="form.gardenName" placeholder="请输入园区运营公司" />
        </el-form-item>
        <el-form-item label="统一信用编码" prop="gardenCode">
          <el-input v-model="form.gardenCode" placeholder="请输入统一信用编码" />
        </el-form-item>
        <el-form-item label="园区人数" prop="gardenNum">
          <el-input v-model="form.gardenNum" placeholder="请输入园区人数" type="number" />
        </el-form-item>
        <el-form-item label="地址" prop="gardenAddress">
          <el-input v-model="form.gardenAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="负责人" prop="gardenPerson">
          <el-input v-model="form.gardenPerson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人联系方式" prop="gardenPhone">
          <el-input v-model="form.gardenPhone" placeholder="请输入负责人联系方式" type="number" />
        </el-form-item>
        <el-form-item label="安全人员" prop="safePerson">
          <el-input v-model="form.safePerson" placeholder="请输入安全人员" />
        </el-form-item>
        <el-form-item label="安全人员联系方式" prop="safePhone">
          <el-input v-model="form.safePhone" placeholder="请输入安全人员联系方式" type="number" />
        </el-form-item>
        <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAqjcGardenInfo, getAqjcGardenInfo, delAqjcGardenInfo, addAqjcGardenInfo, updateAqjcGardenInfo } from "@/api/system/aqjcGardenInfo";
import { getdeptByparentid } from '@/api/system/dept'; // 确保接口路径正确
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import store from "@/store";
import { deptTreeSelect } from '@/api/system/user' // 确保store正确初始化

export default {
  name: "AqjcGardenInfo",
  components: { Treeselect },
  data() {
    return {
      // 部门树选项
      deptOptions: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 园区基础信息表格数据
      aqjcGardenInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        parkImage: null,
        gardenName: null,
        gardenCode: null,
        gardenNum: null,
        gardenAddress: null,
        gardenPerson: null,
        safePerson: null,
        gardenPhone: null,
        safePhone: null,
        lon: null,
        lat: null,
        createId: null,
        updateId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      cascaderValue: [], // 新增：用于存储级联选择器的选中值数组
      props: {
        checkStrictly: true,
        lazy: true,
        async lazyLoad(node, resolve) {
          const parentId = node.value ||
            (['admin', 'adm'].some(r => store.getters.roles.includes(r))
              ? 200
              : store.getters.deptId);

          const res = await getdeptByparentid({
            parentid: parentId,
            deptType: '0'
          });

          resolve(res.data.map(item => ({
            value: item.deptId,
            label: item.deptName,
            leaf: node.level >= 2 // 根据业务层级调整
          })));
        }
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
     // 新增：处理辖区选择变化
     changeXqid(value) {
      this.form.deptId = value[value.length - 1];
      this.cascaderValue = value; // 存储路径用于回显
    },
    /** 查询园区基础信息列表 */
    getList() {
      this.loading = true;
      listAqjcGardenInfo(this.queryParams).then(response => {
        this.aqjcGardenInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        parkImage: null,
        gardenName: null,
        gardenCode: null,
        gardenNum: null,
        gardenAddress: null,
        gardenPerson: null,
        safePerson: null,
        gardenPhone: null,
        safePhone: null,
        lon: null,
        lat: null,
        createId: null,
        createTime: null,
        updateId: null,
        updateTime: null,
        remark: null
      };
      this.cascaderValue = []; // 新增：清空级联路径
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加园区基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAqjcGardenInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改园区基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAqjcGardenInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAqjcGardenInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除园区基础信息编号为"' + ids + '"的数据项？').then(function() {
        return delAqjcGardenInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/aqjcGardenInfo/export', {
        ...this.queryParams
      }, `aqjcGardenInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
